package com.ruoyi.project.cms.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.project.cms.domain.vo.CmsAreaCascaderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.cms.mapper.CmsAreaMapper;
import com.ruoyi.project.cms.domain.CmsArea;
import com.ruoyi.project.cms.service.ICmsAreaService;

/**
 * 行政区划Service业务层处理
 *
 * @author ruoyi
 * @date 2020-10-13
 */
@Service
public class CmsAreaServiceImpl implements ICmsAreaService {
    @Autowired
    private CmsAreaMapper cmsAreaMapper;

    /**
     * 查询行政区划
     *
     * @param id 行政区划ID
     * @return 行政区划
     */
    @Override
    public CmsArea selectCmsAreaById(Long id) {
        return cmsAreaMapper.selectCmsAreaById(id);
    }

    /**
     * 查询行政区划列表
     *
     * @param cmsArea 行政区划
     * @return 行政区划
     */
    @Override
    public List<CmsArea> selectCmsAreaList(CmsArea cmsArea) {
        return cmsAreaMapper.selectCmsAreaList(cmsArea);
    }

    /**
     * 新增行政区划
     *
     * @param cmsArea 行政区划
     * @return 结果
     */
    @Override
    public int insertCmsArea(CmsArea cmsArea) {
        return cmsAreaMapper.insertCmsArea(cmsArea);
    }

    /**
     * 修改行政区划
     *
     * @param cmsArea 行政区划
     * @return 结果
     */
    @Override
    public int updateCmsArea(CmsArea cmsArea) {
        return cmsAreaMapper.updateCmsArea(cmsArea);
    }

    /**
     * 批量删除行政区划
     *
     * @param ids 需要删除的行政区划ID
     * @return 结果
     */
    @Override
    public int deleteCmsAreaByIds(Long[] ids) {
        return cmsAreaMapper.deleteCmsAreaByIds(ids);
    }

    /**
     * 删除行政区划信息
     *
     * @param id 行政区划ID
     * @return 结果
     */
    @Override
    public int deleteCmsAreaById(Long id) {
        return cmsAreaMapper.deleteCmsAreaById(id);
    }

    @Override
    public List<CmsAreaCascaderVo> selectCmsAreaList() {
        return null;
    }

    @Override
    public int deleteAreaByIds(Long[] ids) {
        for (Long id : ids) {
            CmsArea cmsArea = selectCmsAreaById(id);
            cmsArea.setIsDel(Constants.SYS_YES);
            updateCmsArea(cmsArea);
        }
        return 1;
    }

    @Override
    public List<CmsAreaCascaderVo> cmsAreaCascader() {
        List<CmsAreaCascaderVo> srcCityVo = cmsAreaMapper.selectAreaCasaderVo();
        List<CmsAreaCascaderVo> cascaderVos = Lists.newArrayList();
        if (CollUtil.isNotEmpty(srcCityVo)) {
            cascaderVos = fillCascaderVoByParentCode(srcCityVo, "0");
            fill(cascaderVos, srcCityVo);
        }
        return cascaderVos;
    }


    private void fill(List<CmsAreaCascaderVo> cascaderVos, List<CmsAreaCascaderVo> src) {
        if (CollUtil.isNotEmpty(cascaderVos)) {
            for (CmsAreaCascaderVo cascaderVo : cascaderVos) {
                List<CmsAreaCascaderVo> childs = fillCascaderVoByParentCode(src, cascaderVo.getAreaCode());
                if (CollUtil.isNotEmpty(childs)){
                    cascaderVo.setChildren(childs);
                    src.removeAll(childs);
                    fill(childs, src);
                }
            }
        }

    }


    private List<CmsAreaCascaderVo> fillCascaderVoByParentCode(List<CmsAreaCascaderVo> src, String parentCode) {
        return src.stream().filter((CmsAreaCascaderVo m1) -> m1.getParentCode().equals(parentCode)).collect(Collectors.toList());
    }
}
